﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.IO;
using System.Reflection;

using IBatisNet.DataMapper;
using IBatisNet.DataMapper.Configuration;

namespace Pg.BioMedics.SDR.Data.Strategy
{
    /// <summary>
    /// Provides data mapper initilization and managment logic. This startegy 
    /// applies cutom properties to sql mapper during initialization.
    /// </summary>
    internal class CustomDataMapperStrategy : IDataMapperStrategy
    {
        #region Fields

        private ISqlMapper _dataMapper;

        #endregion

        #region Constructors

        /// <summary>
        /// Creates instance of data mapper strategy.
        /// </summary>
        /// <param name="properties">Custom data mapper configuration properties</param>
        internal CustomDataMapperStrategy(NameValueCollection properties)
        {
            try
            {
                Stream stream = Assembly.GetExecutingAssembly()
                                        .GetManifestResourceStream("Pg.BioMedics.SDR.Data.SqlMap.config");

                DomSqlMapBuilder builder = new DomSqlMapBuilder();
                builder.Properties = properties;

                _dataMapper = builder.Configure(stream);
            }
            catch (Exception ex)
            {
                throw new SDRDataException("BaseDataManager : Can't initialize data provider", ex);
            }
        }

        #endregion

        #region IDataMapperStrategy Members

        /// <summary>
        /// Gets initialized data mapper instance
        /// </summary>
        public ISqlMapper DataMapper
        {
            get 
            {
                return _dataMapper;
            }
        }

        #endregion
    }
}
